package demo2;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-05-29
 * Time: 12:44
 */

import java.util.Arrays;

/**
 * 痛点：
 * 1.当前的自定义类 到底要根据什么样的规则 进行比较？
 * 2.这个规则怎么定义？
 * 3.用Comparable接口，缺陷：这个比较方式比较固定，不适合非常灵活的比较。因为如果比较不同的类型，需要写不同类型的实现方法
 *   用途：用在默认的比较上
 */
class Student implements Comparable<Student> {
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //假如以年龄比较
        return this.age - o.age;
        //假设以姓名比较
        //return this.name.compareTo(o.name);
    }
}
public class Test {
    public static void main(String[] args) {
        //实例化一个学生对象
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 17);
        students[1] = new Student("lisi", 12);
        students[2] = new Student("wangwu", 19);
        System.out.println("根据年龄来比较：");
        System.out.println("排序前：" + Arrays.toString(students));
        //实例化一个AgeComparator对象：
        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students, ageComparator);
        //mySort(students);
        //结论：只要是自定义类型涉及到了 大小比较，目前的一个结论是：一定得实现Comparable接口
        System.out.println("排序后：" + Arrays.toString(students));
        System.out.println("======================================================================================================");
        System.out.println("根据年龄来比较：");
        int ret = ageComparator.compare(students[1], students[2]);
        System.out.println(ret);
        System.out.println("======================================================================================================");
        System.out.println("根据姓名来比较：");
        System.out.println("排序前：" + Arrays.toString(students));
        NameComparator nameComparator = new NameComparator();
        Arrays.sort(students, nameComparator);
        System.out.println("排序后：" + Arrays.toString(students));
    }
    //自己实现一个排序，了解一下Arrays.sort()是怎么运行的
    public static void mySort(Comparable[] comparables) {
        //为什么用接口？使用接口相当于数组的类型是Comparable，有Comparable接口的属性
        for (int i = 0; i < comparables.length - 1; i++) {//趟数
            for (int j = 0; j < comparables.length - 1 - i; j++) {//一趟
                //交换：前 > 后 就交换
                if(comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }

        }
    }
    public static void main2(String[] args) {
        //实例化一个学生对象
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan", 17);
        students[1] = new Student("lisi", 12);
        students[2] = new Student("wangwu", 19);
        System.out.println("排序前：" + Arrays.toString(students));
        //Arrays.sort(students);
        mySort(students);
        //结论：只要是自定义类型涉及到了 大小比较，目前的一个结论是：一定得实现Comparable接口
        System.out.println("排序后：" + Arrays.toString(students));
    }
    public static void main1(String[] args) {
        Student student1 = new Student("zhangsan", 19);
        Student student2 = new Student("lisi", 18);
        //问题：怎么比较两个学生的大小，根据年龄？根据名字？
        //1.用age比较 -> 简单点
        //System.out.println(student1.compareTo(student2));
        //谁调用了 这个方法，谁就是this -> this指的就是student1
        //2.用name比较
        System.out.println(student1.compareTo(student2));
    }
}
